package com.liefox.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.liefox.commonutils.R;
import com.liefox.commonutils.exceptionhandler.GuliException;
import com.liefox.vod.Utils.ConstantPropertiesUtil;
import com.liefox.vod.Utils.InitVodClientClass;
import com.liefox.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author zjh
 * @Date 2021/7/17 下午 4:06
 **/
@Api(description = "视频上传")
@RestController
@RequestMapping("/eduvod/video")
//@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;

    @ApiOperation(value = "上传视频到阿里云")
    @PostMapping("/upvod")
    public R upvod(MultipartFile file) {
        String videoId = vodService.upVodToAly(file);
        return R.ok().data("videoId", videoId);
    }

    @ApiOperation(value = "删除视频")
    @DeleteMapping("{videoId}")
    public R removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required = true)
                         @PathVariable String videoId) {
        vodService.removeVideo(videoId);
        return R.ok().message("视频删除成功");
    }

    @ApiOperation(value = "删除多个视频")
    @DeleteMapping("delMore")
    public R delMore(@RequestParam("videoIdList") List videoIdList) {
        vodService.removeMore(videoIdList);
        return R.ok().message("视频删除成功");
    }

    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        try {
            final DefaultAcsClient vodClient =
                    InitVodClientClass.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);

            final GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);

            final GetVideoPlayAuthResponse acsResponse = vodClient.getAcsResponse(request);
            final String playAuth = acsResponse.getPlayAuth();

            return R.ok().data("playAuth",playAuth);


        } catch (Exception e) {
            throw new GuliException(20001,"获取凭证失败");
        }
    }
}
